package com.chuangke.sinotrukbudget.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.admin.model.SysUser;
import com.chuangke.admin.model.SysUserDataDept;
import com.chuangke.admin.service.SysUserDataDeptService;
import com.chuangke.admin.service.SysUserService;
import com.chuangke.common.utils.SecurityUtils;
import com.chuangke.core.http.HttpResult;
import com.chuangke.sinotrukbudget.service.BudgetTodoService;

/**
 * bpc预算上报对象Controller
 * 
 * @author chuangke
 * @date "2022-10-21"
 */
@RestController
@RequestMapping("/budget/plan")
public class BudgetBpcPlanTodoController {

	@Autowired
	private BudgetTodoService bBudgetTodoService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserDataDeptService sysUserDataDeptService ;

	/**
	 * 审批界面的预算查询
	 * 
	 * @param params
	 * @return
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/todo")
	public HttpResult todo() {
		String userId = SecurityUtils.getUserId();
		SysUser user = sysUserService.findById(userId);
		List<SysUserDataDept> userDataDeptList = sysUserDataDeptService.find(userId) ;
		List<String> deptList = userDataDeptList.stream().map(SysUserDataDept::getDeptId).collect(Collectors.toList()) ;
		deptList.add(user.getDeptId()) ;
		return HttpResult.ok(bBudgetTodoService.getTodoCount(userId, deptList));
	}
	
	/**
	 * 获取待修改预算统计
	 * 
	 * @param userDeptList
	 * @return
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/edittask")
	public HttpResult  getNeededEditBudgetTask() {
		String userId = SecurityUtils.getUserId();
		SysUser user = sysUserService.findById(userId);
		List<SysUserDataDept> userDataDeptList = sysUserDataDeptService.find(userId) ;
		List<String> deptList = userDataDeptList.stream().map(SysUserDataDept::getDeptId).collect(Collectors.toList()) ;
		deptList.add(user.getDeptId()) ;
		return HttpResult.ok(bBudgetTodoService.getNeededEditBudgetTask(deptList));
	};

	/**
	 * 获取审批待办任务统计
	 * 
	 * @param userDeptList
	 * @return
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/toaudittask")
	public HttpResult  getTodoAuditBudgetTask() {
		String userId = SecurityUtils.getUserId();
		return HttpResult.ok(bBudgetTodoService.getTodoAuditBudgetTask(userId));
	};

	/**
	 * 获取已办任务统计
	 * 
	 * @param userId
	 * @return
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/donetask")
	public HttpResult  getDoneAuditBudgetTask() {
		String userId = SecurityUtils.getUserId();
		return HttpResult.ok(bBudgetTodoService.getDoneAuditBudgetTask(userId));
	};
	

	/**
	 * 获取已办结任务统计
	 * 
	 * @param userDeptList
	 * @return
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/doneovertask")
	public HttpResult  getDoneAndOverAuditBudgetTask() {
		String userId = SecurityUtils.getUserId();
		return HttpResult.ok(bBudgetTodoService.getDoneAndOverAuditBudgetTask(userId));
	};

	/**
	 * 获取退回任务统计
	 * 
	 * @param userDeptList
	 * @return
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/rejecttask")
	public HttpResult  getRejectAuditBudgetTask() {
		String userId = SecurityUtils.getUserId();
		return HttpResult.ok(bBudgetTodoService.getRejectAuditBudgetTask(userId));
	};

}
